package DataAccess;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import Domain.Account;
import Domain.Catalog;
import Domain.ShoppingCartItem;
import Exception.DadosException;



public class DAOAccount extends DAOGenerico<Account> implements IDAOAccount {

	public DAOAccount() {
		super();
		
	}
	
	
	public List<Account> consultarTodos(){
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("Accenture");
		EntityManager entityManager = emf.createEntityManager();
		
		TypedQuery<Account> tq = entityManager.createQuery("SELECT aa FROM Account aa",Account.class);
			
		List<Account> list = tq.getResultList();
		entityManager.close();
		emf.close();
		return list;
		
				
	}
	
	
	@Override
	public Boolean usernameValid(Account account){
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("Accenture");
		EntityManager entityManager = emf.createEntityManager();
		
		TypedQuery<Account> tq = entityManager.createQuery("SELECT a FROM Account a WHERE a.username=:valor",Account.class);
		tq.setParameter("valor", account.getUsername());
		
		List<Account> list = tq.getResultList();
		
		entityManager.close();
		emf.close();
		
		if(list.size()>0){
			return false;
		}
		
		return true;
		
	}
	@Override
	public Account login(String username, String password){
		

		EntityManagerFactory emf = Persistence.createEntityManagerFactory("Accenture");
		EntityManager entityManager = emf.createEntityManager();
		
		TypedQuery<Account> tq = entityManager.createQuery("SELECT a FROM Account a WHERE a.username=:user AND a.password=:pass",Account.class);
		tq.setParameter("user", username);
		tq.setParameter("pass", password);
		
		List<Account> list = tq.getResultList();
		
		entityManager.close();
		emf.close();
		
		if(list.size()>0){
			return list.get(0);
		}
		
		return null;
	}
	
   public Account consultarPorIdNOVO(Integer id){
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("Accenture");
		EntityManager entityManager = emf.createEntityManager();
		
		
		Account  c = entityManager.find(Account.class, id);
		
		
		entityManager.close();
		emf.close();
		return c;
	}
   
   public final void remover(Account objeto) throws DadosException {

		EntityManagerFactory emf = Persistence.createEntityManagerFactory("Accenture");
		entityManager = emf.createEntityManager();

		EntityTransaction tx = entityManager.getTransaction();
		try {
			tx.begin();

			Query query = entityManager
					.createQuery("DELETE FROM Account c WHERE c= :p");
			int deletedCount = query.setParameter("p", objeto).executeUpdate();

			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null && tx.isActive()) {
				tx.rollback();
			}
			throw new DadosException(DadosException.ERROR_DEL, e);
		}

		entityManager.close();
		emf.close();
	}

}
